package com.chaos.system.controller;


import com.chaos.framework.model.dto.ID;
import com.chaos.framework.model.dto.page.PageQueryDto;
import com.chaos.framework.model.dto.result.data.BooleanResult;
import com.chaos.framework.model.dto.result.data.DataResult;
import com.chaos.framework.model.dto.result.list.ListResult;
import com.chaos.framework.model.dto.result.page.PageResult;
import com.chaos.framework.starter.base.BaseController;
import com.chaos.system.api.data.RbacPermissionMenuBtnData;
import com.chaos.system.dto.RbacPermissionMenuBtnDto;
import com.chaos.system.entity.RbacPermissionMenuBtn;
import com.chaos.system.service.RbacPermissionMenuBtnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jacky.Cui
 * @since 2024-11-13
 */
@Slf4j
@RestController
@Api(tags = "权限菜单按钮关联表")
@RequestMapping("/rbacPermissionMenuBtn")
public class RbacPermissionMenuBtnController extends BaseController<RbacPermissionMenuBtnData> {
    @Resource
    private RbacPermissionMenuBtnService rbacPermissionMenuBtnService;

    @PostMapping("/one.admin")
    @ApiOperation(value = "权限菜单按钮关联表详情", httpMethod = "POST")
    public DataResult<RbacPermissionMenuBtnData> one(@RequestBody ID data) {
        return dataResult(rbacPermissionMenuBtnService.selectByID(data));
    }

    @PostMapping("/list.admin")
    @ApiOperation(value = "权限菜单按钮关联表列表", httpMethod = "POST")
    public ListResult<RbacPermissionMenuBtnData> list(@RequestBody RbacPermissionMenuBtnData data) {
        return listResult(rbacPermissionMenuBtnService.selectByData(data));
    }

    @PostMapping("/page.admin")
    @ApiOperation(value = "权限菜单按钮关联表分页", httpMethod = "POST")
    public PageResult<RbacPermissionMenuBtnData> page(@RequestBody PageQueryDto<RbacPermissionMenuBtnData> data) {
        return pageResult(rbacPermissionMenuBtnService.selectByPage(data));
    }

    @PostMapping("/add.admin")
    @ApiOperation(value = "新增权限菜单按钮关联表", httpMethod = "POST")
    public BooleanResult add(@RequestBody RbacPermissionMenuBtnDto data) {
        List<RbacPermissionMenuBtn> list = data.getPermissionIds().stream().map(permissionId -> RbacPermissionMenuBtn.builder().permissionId(permissionId)
                .menuOrBtnId(data.getMenuOrBtnId()).correlationType(data.getCorrelationType()).build()).collect(Collectors.toList());
        return booleanResult(rbacPermissionMenuBtnService.saveBatch(list));
    }

    @PostMapping("/update.admin")
    @ApiOperation(value = "编辑权限菜单按钮关联表", httpMethod = "POST")
    public BooleanResult update(@RequestBody RbacPermissionMenuBtnData data) {
        return booleanResult(rbacPermissionMenuBtnService.updateModelByID(data));
    }

    @PostMapping("/delete.admin")
    @ApiOperation(value = "删除权限菜单按钮关联表", httpMethod = "POST")
    public BooleanResult delete(@RequestBody ID data) {
        return booleanResult(rbacPermissionMenuBtnService.deleteModel(data));
    }

}
